<?php

namespace App\Models;

use Validator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Article extends Model
{
    //软删除
    use SoftDeletes;

    protected $datas = ['created_at','updated_at','deleted_at'];
    protected $fillable = ['title','tags','member_id','cate_id','is_top','status','desc','content'];
    protected $dateFormat = 'U';

    //关联导航的模型
    public function cates ()
    {
        return $this->belongsTo('App\\Models\\Cate','cate_id','id');
    }

    //关联会员模型
    public function members ()
    {
        return $this->belongsTo('App\\Models\\Member','member_id','id');
    }

    //文章添加
    public function add ($data)
    {
        $rule = [
            'title' => 'bail|required',
            'tags'=> 'required',
            'member_id' => 'required',
            'cate_id' => 'required',
            'desc' => 'required',
            'content' => 'required',
        ];

        $msg = [
            'title.required' => '标题不能为空',
            'tags.required' => '标签不能为空',
            'member_id.required' => '作者不能为空',
            'cate_id.required' => '栏目不能为空',
            'desc.required' => '描述不能为空',
            'content.required' => '内容不能为空'
        ];

        $validator = Validator::make($data,$rule,$msg);
        if ($validator->fails()) {
            return $validator->errors()->first();
        }

        $result = $this->create($data);
        if ($result) {
            return 1;
        } else {
            return "添加失败";    
        }
    }
}
